********************************************************************************
** Set Simulation Parameters
********************************************************************************

* 1. Set reference year
local refYear 2007

********************************************************************************
** Build Simulation Dataset
********************************************************************************

*** 1. DOMESTIC EI ESTIMATES (time variant)
{
use "$EI/energy_intensity_industry_year.dta", clear

keep naics2012 industryname2012 year intensity intensity_ppidef intensity_2
rename naics2012 naics
rename intensity ei_tv
rename intensity_ppidef ei_ppidef_tv
rename intensity_2 ei_2_tv

tempfile ei_tv
save `ei_tv.dta', replace

}

*** 2. MERGE IN REGRESSION VARS & BUILD NEW VARS
* (1) import regression dataset
use "$buildpath/output/regressions_dataset.dta", clear
* Keep relevant variables in reference year
keep if year == `refYear' 

* (2) merge in domestic energy intensities 
merge 1:1 naics year using `ei_tv.dta', keep(1 3) nogen

* Generate domestic emission variables
gen us_co2_rate_direct_combust = ci_tvshare * ei_tv /1000  // tons per million dollars (2007)
label var us_co2_rate_direct_combust "tons per million dollars (2007)"
gen us_co2_rate_indirect_combust = ci_tvshare * ei_tot // tons per million dollars (2007)
label var us_co2_rate_indirect_combust "tons per million dollars (2007)"


* domestic energy factor 
gen e_d = us_co2_rate_direct_combust
label var e_d "tons per million dollars (2007)"

gen e_f=us_co2_rate_indirect_combust
label var e_f "total tons per million dollars (2007)"

gen emissions_bau= shipments*us_co2_rate_direct_combust
label var emissions_bau "tons"


* (2) foreign energy factor using domestic
gen e_imp = e_d
gen e_exp = e_d
	
* save simulation dataset
order naics industryname2012 year
order ei_tv ei_ppidef_tv ei_2_tv, a(ei_2)
drop wgt*

save "$result_simul/simulation_dataset.dta", replace
